<?php

session_start ();
header ( "Cache-control: private" );

if ($_SESSION ['docid'] == "1") {
	$admin = 1;
}

//if ($_SESSION['docid'] == "1") { $admin=1; $inad = ",notes";}


include 'includes/functions.php';
echo db_connect ();

$search_term = $_GET ['search'];

$ids = getIDs ( 'title', $search_term );
$temp = getIDs ( 'detail_title', $search_term );
$ids = array_merge ( $ids, $temp );
$temp = getIDs ( 'body', $search_term );
$ids = array_merge ( $ids, $temp );


if ($admin) {
  $temp = getIDs ( 'notes', $search_term );
  $ids = array_merge ( $ids, $temp );
}

//DIE(print_r($ids));

//  TODO: go back to using a fulltext search, debug why 'client' was showing nothing 
//$sql = "select * from documentation_sections where match(body,title, detail_title$inad) against('".$search_term."*')";


function getIDs($column, $string) {
	$query = 'SELECT section_id FROM documentation_sections WHERE `' . $column . '` LIKE "%' . $string . '%" AND documentation_chapter_id != "34"';
	if (! $result = mysql_query ( $query ))
		DIE ( "error getting ids" );
	while ( $row = mysql_fetch_array ( $result ) ) {
		$arr [] = $row [0];
	}
	return $arr;
}




foreach ( $ids as $my_id ) {
	$sql = "select * from documentation_sections where section_id = '$my_id'";
	
	$result = mysql_query ( $sql ); //echo $sql;
	$row = mysql_fetch_array ( $result );
	$id = $row [0];
	$Cid = $row [1];
	$Csql = "select title from documentation_chapters
		 WHERE documentation_chapter_id = '$Cid' LIMIT 1";
	$Cresult = mysql_query ( $Csql );
	if ($Crow = mysql_fetch_array ( $Cresult )) {
		$chapter_title = $Crow [0];
	} else {
		DIE ( mysql_error () . $Csql );
	}
	
	$list_arr [$id] = array ("chapter" => $chapter_title, "section" => $row [2], "detail_title" => $row [6], "body" => stripslashes ( $row [3] ), "notes" => stripslashes ( $row [10] ) );
	
//$templates[$row3[0]] = array("body" => $row3[1], "subject" => $row3[2]); 


} // foreach


?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="includes/index.js"></script>
<script type="text/javascript" charset="utf-8">
	$(document).ready(function(){
	   
		 });
		
</script>
<title>Search</title>
<link rel="stylesheet" href="css/app_styles.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css" media="screen">
.list_item {
	padding-bottom: 20px;
}

.context {
	font-weight: bold;
	color: black;
	font-size: 14px;
	font-weight: bold;
	padding:5px;
}

#header {
	font-weight: bold;
	color: white;
	padding: 5px;
	font-size: 18px;
	background-color: green;
	margin-bottom: 15px
}

.body_item {
	overflow: auto;
	max-height: 100px;
	width: 700px;
	position: relative;
	font-size: 9px;
	font-weight: normal;
	padding: 3px;
	background-color: #eee;
	display: inline-block;
}

.hilite {
	padding: 1px;
	background-color: #feffd4;
}

.notess {
	font-size: 10px;
	display: inline-block;
	vertical-align: top;
}
</style>
</head>

<body>

<div id='header'><a onClick='history.go(-1);'
	style='font-size: 14px; color: white; cursor: pointer; text-decoration: underline;'>Back to Manual</a> : Search results for '<?=$search_term?>.'</div>
<div id="list">
<?php
if (! $list_arr) {
	echo "No results for '" . $search_term . "'. Please <a onClick='history.go(-1);'><span style='text-decoration: underline; color:blue;'>try again.</span></a>";
} else {
	foreach ( $list_arr as $key => $val ) {
		$context = $val ['chapter'] . " > " . $val ['section'];
		if ($val ['detail_title'])
			$context .= " > <strong>" . $val ['detail_title'] . "</strong>";
		$body = $val ['body'];	
		$body = str_replace("<br>", "[[xx]]", $body);
		$body = str_replace("</p>", "[[xx]]", $body);	
		$body = strip_tags($body);	
		$body = substr ( $body, 0, 5000 );
		$body = str_replace("[[xx]]", "<div style='height:3px;'></div>", $body);	
		$body = str_replace ( $search_term, "<span class='hilite'>$search_term</span>", $body );
		$body = parse_crossReference ($body,0,0, 1);
		
		if ($admin) {
			$notess = substr ( $val ['notes'], 0, 1000 );
			$notess = str_replace ( $search_term, "<span class='hilite'>$search_term</span>", $notess );
			$notes_ttl = "Notes:<br>";
		} else {
			$notess = "";
			$notes_ttl = "";
		}
		echo "<div class='wrapper'>
			<div class='list_item'>
				<div class='context'><a href='index.php?s=$key&f=x'>$context</a></div>
				<div class='body_item'>$body</div>
				<div class='notess'>$notes_ttl$notess</div>
	      </div>
	</div>
	";
	
	}
} // end of else
?>
</div>
<div id="bg_blanker"></div>

</body>
</html>